package com.shaozi.nc.manage.observer;

import java.util.Observable;
import java.util.Observer;

import org.springframework.beans.factory.annotation.Autowired;

import com.shaozi.data.mq.business.base.SourceTypeConsts.OA.Report;
import com.shaozi.data.mq.business.observer.MqTargetObserverBean;
import com.shaozi.nc.manage.observer.base.BaseSecretaryObserver;
import com.shaozi.nc.manage.observer.manage.ReportRuleManage;
import com.shaozi.nc.mq.bean.report.RuleUpdateBean;

/**
 * 汇报观察者 https://shimo.im/doc/BESz6u08M9Ap3Ju7
 * 
 * @author lyx
 *
 */
public class ReportObserver extends BaseSecretaryObserver implements Observer {

	@Autowired
	private ReportRuleManage reportRuleManage;
	// 提交汇报 6003,6004,6005
	private static final Integer[] Recv_New_sourceType = { Report.AlarmByOther, Report.Alarm_New, Report.Alarm_End };
	// 查看汇报 6012,6013，6014
	private static final Integer[] Rule_Look_Report_sourceType = { Report.Comment, Report.Reply_Comment,
			Report.Recv_New };
	// 评论/回复评论 6012,6013
	private static final Integer[] Reply_or_Comment_sourceType = { Report.Comment, Report.Reply_Comment };

	@Override
	public void update(Observable o, Object arg) {
		if (arg != null) {
			if (arg instanceof MqTargetObserverBean) {
				MqTargetObserverBean mqBean = (MqTargetObserverBean) arg;
				int sourceType = mqBean.getSourceType();
				switch (sourceType) {
				case Report.Rule_Update:
					ruleUpdate(mqBean);
					break;
				case Report.Recv_New:// 有人发来汇报
					updateUserRead(sourceType, mqBean, Recv_New_sourceType);
					break;
				case Report.Rule_Look_Report:// 查看工作汇报
					updateUserRead(sourceType, mqBean, Rule_Look_Report_sourceType);
					break;
				case Report.Comment:
				case Report.Reply_Comment:// 评论/回复评论
					updateUserRead(sourceType, mqBean, Reply_or_Comment_sourceType);
					break;
				default:
					break;
				}
			}
		}
	}

	private void ruleUpdate(MqTargetObserverBean mqBean) {
		if (mqBean.getBiz() instanceof RuleUpdateBean) {
			RuleUpdateBean obj = (RuleUpdateBean) mqBean.getBiz();
			if (obj != null) {
				reportRuleManage.updateRule2Job(mqBean, obj);
			}
		}
	}

}
